#! @BASH@
# -*- mode: sh -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Written by Victor Ananjevsky, 2010
#

VERSION=@VERSION@
GETTEXT_DOMAIN=@PACKAGE@

file=$1
if [[ -z $file || $file == "-h" || $file == "--help" ]]; then
    echo "Usage: $(basename $0) <filename>"
    exit 1
fi
if [[ $file == "-v" || $file == "--version"  ]]; then
    echo $"${0##*/}, version - $VERSION"
    exit 0
fi

eval $(mtag --utf --shell "$file")

exec 3>&1
vals=$(@DIALOG@ --clear --no-shadow \
    --backtitle $"Media Tag Editor (by Victor Ananjevsky, 2010)" \
    --ok-label $"Save" --cancel-label $"Exit" --title $" Tag Editor " \
    --form $"Tags for file: '$file'" 21 60 0 \
    $"Artist:"    1 2 "$MTAG_ARTIST"   1 12 40 1024 \
    $"Title:"     3 2 "$MTAG_TITLE"    3 12 40 1024 \
    $"Album:"     5 2 "$MTAG_ALBUM"    5 12 40 1024 \
    $"Year:"      7 2 "$MTAG_YEAR"     7 12 40 0 \
    $"Comments:"  9 2 "$MTAG_COMMENT"  9 12 40 1024 \
    $"Genre:"    11 2 "$MTAG_GENRE"   11 12 40 1024 \
    $"Track:"    13 2 "$MTAG_TRACK"   13 12 40 0 \
    2>&1 1>&3)
ret=$?
exec 3>&-

if [[ $ret -eq 0 ]]; then
    eval tags=($(echo "$vals" | sed 's/.*/"&"/'))
    mtag -s -u -a "${tags[0]}" -t "${tags[1]}" -A "${tags[2]}" -y "${tags[3]:-0}" -c "${tags[4]}" -g "${tags[5]}" -T "${tags[6]:-0}" "$file"
fi
